package com.example.mobifin_busticket.views.activities;

import java.util.ArrayList;

import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.R.id;
import com.example.mobifin_busticket.R.layout;
import com.example.mobifin_busticket.R.menu;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NewCancelTicketSuccessScreen extends MobifinActivity implements
		OnClickListener {
	TextView tvPNR, tvStatus, tvTicketNo, tvDeparturetime, tvTravelsName,
			tvTovalue, tvFromValue, tvCancellationCharge, tvRefundAmt,
			tvTotalFare, tvJournyDate, tvpassenger, tvpassengerage,
			tvpassengergender, tvseatno, tvticketStatus;
	ArrayList<String> passengerList;
	View inflatedLayout;
	LinearLayout llPassengerMain;
	Button btn_ok, btn_headerBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_cancel_ticket_success_screen);
		passengerList = new ArrayList<String>();
		tvPNR = (TextView) findViewById(R.id.tvPNR);
		tvStatus = (TextView) findViewById(R.id.tvStatus);
		tvTicketNo = (TextView) findViewById(R.id.tvTicketNo);
		tvDeparturetime = (TextView) findViewById(R.id.tvDeparturetime);
		tvTravelsName = (TextView) findViewById(R.id.tvTravelsName);
		tvTovalue = (TextView) findViewById(R.id.tvTovalue);
		tvFromValue = (TextView) findViewById(R.id.tvFromValue);
		tvJournyDate = (TextView) findViewById(R.id.tvJournyDate);
		tvCancellationCharge = (TextView) findViewById(R.id.tvCancellationCharge);
		tvRefundAmt = (TextView) findViewById(R.id.tvRefundAmt);
		tvTotalFare = (TextView) findViewById(R.id.tvTotalFare);
		llPassengerMain = (LinearLayout) findViewById(R.id.llPassengerMain);
		btn_ok = (Button) findViewById(R.id.btn_ok);
		btn_headerBack = (Button) findViewById(R.id.btn_headerBack);
		btn_headerBack.setOnClickListener(this);
		btn_ok.setOnClickListener(this);
		setValueInView();
	}

	private void setValueInView() {
		// TODO Auto-generated method stub
		tvPNR.setText(getIntent().getStringExtra("PNR"));
		tvStatus.setText(getIntent().getStringExtra("Status"));
		tvTicketNo.setText(getIntent().getStringExtra("TicketNo"));
		tvDeparturetime.setText(getIntent().getStringExtra("DOJ"));
		tvTravelsName.setText(getIntent().getStringExtra("Travels"));
		tvTovalue.setText(getIntent().getStringExtra("Sorce"));
		tvFromValue.setText(getIntent().getStringExtra("Destination"));
		tvJournyDate.setText(getIntent().getStringExtra("DateOfJourny"));
		tvCancellationCharge.setText(getIntent().getStringExtra(
				"CancellationCharge"));
		tvRefundAmt.setText(getIntent().getStringExtra("RefundAmt"));
		tvTotalFare.setText(getIntent().getStringExtra("TotalFare"));
		passengerList = getIntent().getStringArrayListExtra("PassengerList");
		addDynamicPassenger(passengerList);
	}

	private void addDynamicPassenger(ArrayList<String> passengerList) {
		// TODO Auto-generated method stub
		for (int i = 0; i < passengerList.size(); i++) {

			LayoutInflater inflater = LayoutInflater
					.from(NewCancelTicketSuccessScreen.this);
			inflatedLayout = inflater.inflate(
					R.layout.passenger_row_cancel_details, null);

			String[] strSeattocancel = passengerList.get(i).split(";");
			String[] strSeattocanceldigit = strSeattocancel[1].trim().split(
					"\\|");
			String[] strAgeGender = strSeattocanceldigit[0].split(" ");
			String[] strSeattocandigit = strSeattocanceldigit[1].trim().split(
					":");
			tvpassenger = (TextView) inflatedLayout
					.findViewById(R.id.tvTravellerName);
			tvpassengerage = (TextView) inflatedLayout
					.findViewById(R.id.tvTravllerAge);
			tvpassengergender = (TextView) inflatedLayout
					.findViewById(R.id.tvTravellerGender);
			tvseatno = (TextView) inflatedLayout
					.findViewById(R.id.tvTravellerSeatName);
			tvticketStatus = (TextView) inflatedLayout
					.findViewById(R.id.tvCancelOrBooked);
			tvpassenger.setText("" + strSeattocancel[0]);
			tvpassengerage.setText(strAgeGender[0]);
			tvpassengergender.setText(strAgeGender[1]);
			tvseatno.setText(strSeattocandigit[1].trim());
			tvticketStatus.setText("CANCELLED");
			tvticketStatus.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.button_round_cancel_bk));
			llPassengerMain.addView(inflatedLayout);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_headerBack:
			onClickBackOrOk();
			break;
		case R.id.btn_ok:
			onClickBackOrOk();
			break;
		default:
			break;
		}
	}

	private void onClickBackOrOk() {
		// TODO Auto-generated method stub
		finish();
		Intent backIntent = new Intent(NewCancelTicketSuccessScreen.this,
				SearchBusAcitivity.class);
		backIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(backIntent);
	}
}
